package com.showde.shoppingmall.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.ActivityGroup;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.showde.shoppingmall.R;
import com.showde.shoppingmall.adapter.ShoppingcartAdapter;
import com.showde.shoppingmall.db.ShoppingcartDBCommand;
import com.showde.shoppingmall.domain.ShoppingCart;
import com.showde.shoppingmall.utils.ActivityDispatch;
import com.showde.shoppingmall.utils.ToastUtils;

public class ShoppingcarActivity extends BaseActivity {

	private static final String TAG = "ShoppingcarActivity";

	private ListView listView;
	private ImageView imageViewModify;
	private TextView totalTextView; // 购物车总计

	private ShoppingcartAdapter adapter;
	private ShoppingcartDBCommand shoppingcartDBCommand;

	private float totalFee; // 费用总计

	List<ShoppingCart> shoppingCarts;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_modify:
			if (adapter == null) {
				ToastUtils.showToast(this, R.string.msg_empty_shoppingcart);
				return;
			}
			ArrayList<String> checkedProductIds = adapter
					.getChechedProductIds();
			if (checkedProductIds == null || checkedProductIds.size() == 0) {
				ToastUtils.showToast(this, R.string.msg_not_checkedanyproduct);
				return;
			}
			Bundle bundle = new Bundle();
			bundle.putSerializable(ActivityDispatch.PRODUCT_ID_ARRAY,
					checkedProductIds);
			/*
			 * ActivityDispatch.openNewActivity(this,
			 * ShoppingcartModifyActivity.class,
			 * ActivityDispatch.getActivityGroup(groupTag),
			 * "ShoppingcarActivity", bundle, groupTag);
			 */
			ActivityDispatch.toShoppingcartModify(this, checkedProductIds);

			break;
		}
	}

	@Override
	protected void initParam() {
		if (shoppingcartDBCommand == null) {
			shoppingcartDBCommand = new ShoppingcartDBCommand(this);
		}
		ActivityGroup activityGroup = ActivityDispatch
				.getActivityGroup(groupTag);
		if (activityGroup == null) {
			groupTag = ActivityDispatch.GROUP_SHOPPINGCART;
		}
	}

	@Override
	protected void initView() {
		setContentView(R.layout.activity_shoppingcart);

		imageViewModify = (ImageView) findViewById(R.id.img_modify);
		imageViewModify.setOnClickListener(this);

		totalTextView = (TextView) findViewById(R.id.txt_total);

		listView = (ListView) findViewById(R.id.lst_shoppingcart_pro);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ShoppingCart shoppingCart = shoppingCarts.get(position);
				Bundle bundle = new Bundle();
				bundle.putString(ActivityDispatch.PRODUCT_ID,
						shoppingCart.getProductId());
				ActivityDispatch.openNewActivity(ShoppingcarActivity.this,
						ProductDetailsActivity.class,
						ActivityDispatch.getActivityGroup(groupTag),
						"ShoppingcarActivity", bundle, groupTag);
			}
		});

	}

	@Override
	protected void onResume() {
		super.onResume();
		fillAdapter();
		showToatalFee();
		Log.d(TAG, "onResume");
	}

	private void fillAdapter() {
		shoppingCarts = shoppingcartDBCommand.queryAll();
		if (shoppingCarts == null) {
			shoppingCarts = new ArrayList<ShoppingCart>();
		}
		adapter = (ShoppingcartAdapter) listView.getAdapter();
		if (adapter == null) {
			adapter = new ShoppingcartAdapter(this, shoppingCarts);
			listView.setAdapter(adapter);
		} else {
			adapter.resetData(shoppingCarts);
		}
		adapter.notifyDataSetChanged();
	}

	private void showToatalFee() {
		totalFee = shoppingcartDBCommand.getShoppingcarTotalFee();
		RelativeLayout layout = (RelativeLayout) findViewById(R.id.rl_bottom);
		if (totalFee == 0) {
			layout.setVisibility(View.GONE);
		} else {
			totalTextView.setText("￥" + totalFee);
			layout.setVisibility(View.VISIBLE);
		}
	}

}
